# 多阶段构建 - 构建阶段
FROM maven:3.8.6-openjdk-11-slim AS builder

# 设置工作目录
WORKDIR /app

# 复制Maven配置文件
COPY pom.xml .

# 下载依赖（利用Docker缓存层）
RUN mvn dependency:go-offline -B

# 复制源代码
COPY src ./src

# 构建应用
RUN mvn clean package -DskipTests -B

# 运行阶段
FROM openjdk:11-jre-slim

# 设置维护者信息
LABEL maintainer="Data Gateway Team"
LABEL description="基于Apache Camel的数据接入网关"
LABEL version="1.0.0"

# 设置环境变量
ENV JAVA_OPTS="-Xms512m -Xmx2g -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+UseStringDeduplication"
ENV SPRING_PROFILES_ACTIVE=prod
ENV TZ=Asia/Shanghai

# 创建应用用户
RUN groupadd -r datagateway && useradd -r -g datagateway datagateway

# 创建应用目录
RUN mkdir -p /app/logs /app/config /app/scripts && \
    chown -R datagateway:datagateway /app

# 设置工作目录
WORKDIR /app

# 从构建阶段复制JAR文件
COPY --from=builder /app/target/data-gateway-1.0.0.jar app.jar

# 复制配置文件
COPY --chown=datagateway:datagateway src/main/resources/application.yml config/
COPY --chown=datagateway:datagateway src/main/resources/application-prod.yml config/
COPY --chown=datagateway:datagateway src/main/resources/scripts/ scripts/

# 复制启动脚本
COPY --chown=datagateway:datagateway docker/entrypoint.sh entrypoint.sh
RUN chmod +x entrypoint.sh

# 切换到应用用户
USER datagateway

# 暴露端口
EXPOSE 8080 9090

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:8080/data-gateway/api/health/status || exit 1

# 设置启动命令
ENTRYPOINT ["./entrypoint.sh"]
